package com.judian.smart.util;

import android.content.Context;
import android.graphics.Color;
import android.os.Looper;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.judian.smart.R;
import com.judian.smart.activity.MainActivity;
import com.judian.smart.base.MyApplication;


/**
 * 自定义Toast显示，可控制连续显示toast延时的问题
 *
 * @auther yyx
 */
public class ToastPlus {
    static Toast toast;
    private static View successView;
    private static View erroView;
    private static TextView successText;
    private static TextView erroText;
    private static Context context;
    private static Toast erro;


    public static void showLongSuccess(String msg) {
        showYes(msg, Toast.LENGTH_LONG);
    }

    public static void showSuccess(String msg) {
        showYes(msg, Toast.LENGTH_SHORT);
    }

    public static void showError(String msg) {
        showNo(msg, Toast.LENGTH_SHORT);
    }

    public static void showLongEorro(String msg) {
        showNo(msg, Toast.LENGTH_LONG);
    }

    /**
     * Toast show
     *
     * @param msg        toast消息
     * @param slowLength 显示时长
     */
    private static void showYes(final String msg, int slowLength) {
        context = MyApplication.getInstance();

        if (successView == null) {
            successView = LayoutInflater.from(context).inflate(R.layout.toast_plus_successlayout, null);
            successText = (TextView) successView.findViewById(R.id.toast_text);
            successText.setTextColor(Color.WHITE);
        }
        if (toast == null)
            toast = new Toast(context);

        //设置toast显示位置CENTER_HORIZONTAL -> X轴居中 BOTTOM -> 底部  y轴偏移80
        toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM, 0, 180);
        toast.setDuration(slowLength);
        toast.setView(successView);
        if (Looper.getMainLooper() == Looper.myLooper()) {
            //判断是不是主线程
            successText.setText(msg);

            toast.show();
        } else {
            MainActivity.mInstance.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    successText.setText(msg);

                    toast.show();
                }
            });
        }

    }

    private static void showNo(final String msg, int slowLength) {
        context = MyApplication.getInstance();
        if (erroView == null) {
            erroView = LayoutInflater.from(context).inflate(R.layout.toast_plus_errolayout, null);
            erroText = (TextView) erroView.findViewById(R.id.toast_text);
            erroText.setTextColor(Color.WHITE);
        }

        if (erro == null)
            erro = new Toast(context);
        //设置toast显示位置CENTER_HORIZONTAL -> X轴居中 BOTTOM -> 底部  y轴偏移80
        erro.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM, 0, 180);
        erro.setDuration(slowLength);
        erro.setView(erroView);
        erro.show();
        if (Looper.getMainLooper() == Looper.myLooper()) {
            //判断是不是主线程
            erroText.setText(msg);

            erro.show();
        } else {
            MainActivity.mInstance.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    erroText.setText(msg);

                    erro.show();
                }
            });
        }
    }


}
